/*
 * CRF.h
 *
 *  Created on: Sep 7, 2012
 *      Author: v-suqil
 */

#ifndef CRF_H_
#define CRF_H_
#include <stdlib.h>

#include "InstanceList.h"
#include "util.h"
#include "lbfgs.h"


struct feat {
	int weight;
	double value;
	double expect;
	feat() :
			weight(0), value((double)rand()/RAND_MAX), expect(0) {
	}
};

typedef std::map<int, feat> par_t;
typedef std::vector<double> var_t;

class CRF {
private:
	par_t** pars;//[t][t-1]
	par_t* inipars;
	par_t* finpars;
	int numvars;
	int numStates;
	double GV;//Gaussian variance
	double lone;//l1 norm parameter
	var_t* vars;
	var_t* grads;
	InstanceList* modellist;
	bool built;
	double logLike(Instance* inst);
	double getWeight(value_t* featvec, int i, int j);
	double getInitialWeight(value_t* featvec, int i);
	double getFinalWeight(value_t* featvec, int i);
	double totalValue(Instance* inst);
	double constrainValue(Instance* inst);
	double gaussian();
	double l1();
public:
	CRF(InstanceList* trainlist);
	void build();
	void train();
	void train(int maxiter);
	par_t** getParameters();
	par_t* getInitialParameters();
	par_t* getFinalParameters();
	unsigned getNumVars();
	double* getVars();
	var_t* getVariables();
	void setParameters();
	double getValue();
	var_t* getGradients();
	void setGradients();
	int getNumStates();
	virtual ~CRF();
};

#endif /* CRF_H_ */
